package com.testmanager.testmananger.shuiyou.szjk.service;

import com.alibaba.fastjson.JSONObject;
import com.testmanager.testmananger.foundation.util.FileUtils;
import com.testmanager.testmananger.shuiyou.szjk.domain.ChangeExpressionEntity;
import com.testmanager.testmananger.shuiyou.szjk.domain.ChangeRecordEntity;
import com.testmanager.testmananger.shuiyou.szjk.dto.ChangeExpressionDto;
import com.testmanager.testmananger.shuiyou.szjk.dto.ZipFileDto;
import com.testmanager.testmananger.shuiyou.szjk.mapper.ChangeExpressionMapper;
import com.testmanager.testmananger.shuiyou.szjk.repository.ChangeExpressionRepository;
import com.testmanager.testmananger.shuiyou.szjk.repository.ChangeRecordRepository;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class ZipAndSendService {

    @Autowired
    ChangeRecordRepository changeRecordRepository;

    @Autowired
    ChangeExpressionRepository changeExpressionRepository;


    /**
     * 存储
     * @param list
     * @param zipid
     * @return
     */
    public ResponseEntity saveFileChangeExpressions(List<ChangeExpressionDto> list,long zipid,String title){
        log.info("saveFileChangeExpressions,zipid:{},title:{},list:{}",zipid,title,list);

        ChangeRecordEntity changeRecordEntity = new ChangeRecordEntity();
        changeRecordEntity.setCaseName(title)
                .setZipId(zipid).setPath("");
        ChangeRecordEntity changeRecord = changeRecordRepository.save(changeRecordEntity);
        list.stream().forEach(e->{
            saveFileChangeExpression(e,changeRecord.getId());
        });
        return ResponseEntity.ok().build();

    }

    public ResponseEntity saveFileChangeExpression(ChangeExpressionDto changeExpressionDto,long id){
        log.info("存储chaneexpression:{},recordid:{}",changeExpressionDto,id);
        ChangeExpressionEntity changeExpressionEntity = ChangeExpressionMapper.toChangeExpressionEntity(changeExpressionDto, id);
        ChangeExpressionEntity save = changeExpressionRepository.save(changeExpressionEntity);
        return ResponseEntity.ok().build();
    }

    public  JSONObject  zipFiles(long zipid,String title, List<ZipFileDto> zipFileDtos){

        String caseName = title;
        if ( null ==caseName  ||caseName == ""){
            caseName = "no_title";
        }
         InputStream in = null;
        String fileName = System.currentTimeMillis()+"_"+zipid+".zip";
        String trueFileName = FileUtils.SendSavePath+fileName ;
        File file = new File(trueFileName);
        if (file.getParentFile() != null && !file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }


        try {
//            ChangeRecordEntity changeRecordEntity = new ChangeRecordEntity();
//            changeRecordEntity.setCaseName(caseName).setZipId(zipid).setPath(trueFileName);
//            changeRecordRepository.save(changeRecordEntity);

            ZipFile zipFile = new ZipFile(trueFileName);

          //  in = new ByteArrayInputStream(xml.getBytes());
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
            parameters.setSourceExternalStream(true);

            for (ZipFileDto zipFileDto : zipFileDtos){
                long zipfiledtoid = zipFileDto.getId();
               // if (zipFileDto.getId() == -20 || zip)
//                if (-10 == zipfiledtoid || -20 == zipfiledtoid){ //去除redis 和oracle
//                    continue;
//                }
                in = new ByteArrayInputStream(zipFileDto.getContent().getBytes());
                parameters.setFileNameInZip(zipFileDto.getName());
                zipFile.addStream(in,parameters);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("path",trueFileName);
        jsonObject.put("filename",fileName);

        return jsonObject;
    }



}
